home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / xinu.arc / XINU4.C < prev    next >
Text File  |  1986-01-03  |  11KB  |  402 lines

  1. /* ssclock.c - stopclk, strtclk  p. 134 */
  2.  
  3. # include    <conf.h>
  4. # include    <kernel.h>
  5. # include    <proc.h>
  6. # include    <q.h>
  7. # include    <sleep.h>
  8.  
  9. /*-----------------------------------------------------------------------------
  10.  *  stopclk  --  put the clock in defer mode
  11.  *-----------------------------------------------------------------------------
  12.  */
  13. stopclk()
  14. {
  15.     defclk++;
  16. }
  17.  
  18. /*-----------------------------------------------------------------------------
  19.  *  strtclk  --  take the clock out of defer mode
  20.  *-----------------------------------------------------------------------------
  21.  */
  22. strtclk()
  23. {
  24.     char    ps;
  25.     int    makeup;
  26.     int    next;
  27.  
  28.     disable(ps);
  29.     if ( defclk<=0 || --defclk>0 )  {
  30.         restore(ps);
  31.         return;
  32.     }
  33.     makeup = clkdiff;
  34.     preempt -= makeup;
  35.     clkdiff = 0;
  36.     if ( slnempty )  {
  37.         for (next=firstid(clockq) ;
  38.              next < NPROC && q[next].qkey < makeup ;
  39.              next=q[next].qnext)  {
  40.             makeup -= q[next].qkey;
  41.             q[next].qkey = 0;
  42.         }
  43.         if (next < NPROC)
  44.             q[next].qkey -= makeup;
  45.         wakeup();
  46.     }
  47.     if (preempt <= 0)
  48.         resched();
  49.     restore(ps);
  50. }
  51. /* suspend.c - suspend  p. 69 */
  52.  
  53. # include    <conf.h>
  54. # include    <kernel.h>
  55. # include    <proc.h>
  56.  
  57. /*-----------------------------------------------------------------------------
  58.  * suspend  --  suspend a process, placing it in hibernation
  59.  *-----------------------------------------------------------------------------
  60.  */
  61. SYSCALL    suspend(pid)
  62.     int    pid;            /* id of process to suspend          */
  63. {
  64.     struct pentry    *pptr;        /* pointer to process table entry    */
  65.     char    ps;            /* saved processor status            */
  66.     int    prio;            /* priority returned                 */
  67.  
  68.     disable(ps);
  69.     if (isbadpid(pid) || pid==NULLPROC ||
  70.      ((pptr= &proctab[pid])->pstate !=PRCURR && pptr->pstate !=PRREADY))  {
  71.         restore(ps);
  72.         return(SYSERR);
  73.     }
  74.     if (pptr->pstate == PRREADY)  {
  75.         dequeue(pid);
  76.         pptr->pstate = PRSUSP;
  77.     }  else  {
  78.         pptr->pstate = PRSUSP;
  79.         resched();
  80.     }
  81.     prio = pptr->pprio;
  82.     restore(ps);
  83.     return(prio);
  84. }
  85. /* ttycntl.c - ttycntl  p. 182 */
  86.  
  87. # include    <conf.h>
  88. # include    <kernel.h>
  89. # include    <tty.h>
  90. # include    <io.h>
  91. # include    <slu.h>
  92.  
  93. /*-----------------------------------------------------------------------------
  94.  *  ttycntl  --  control a tty device by setting modes
  95.  *-----------------------------------------------------------------------------
  96.  */
  97. ttycntl(devptr, func, addr)
  98.     struct devsw    *devptr;
  99.     int    func;
  100.     char    *addr;
  101. {
  102.     register struct tty    *ttyp;
  103.     char    ch;
  104.     char    ps;
  105.  
  106.     ttyp = &tty[devptr->dvminor];
  107.     switch ( func )  {
  108.         case TCSETBRK:
  109.             ttyp->ioaddr->ctstat |= SLUTBREAK;
  110.             break;
  111.         case TCRSTBRK:
  112.             ttyp->ioaddr->ctstat &= ~SLUTBREAK;
  113.             break;
  114.         case TCNEXTC:
  115.             disable(ps);
  116.             wait(ttyp->isem);
  117.             ch = ttyp->ibuff[ttyp->itail];
  118.             restore(ps);
  119.             signal(ttyp->isem);
  120.             return(ch);
  121.         case TCMODER:
  122.             ttyp->imode = IMRAW;
  123.             break;
  124.         case TCMODEC:
  125.             ttyp->imode = IMCOOKED;
  126.             break;
  127.         case TCMODEK:
  128.             ttyp->imode = IMCBREAK;
  129.             break;
  130.         case TCECHO:
  131.             ttyp->iecho = TRUE;
  132.             break;
  133.         case TCNOECHO:
  134.             ttyp->iecho = FALSE;
  135.             break;
  136.         case TCICHARS:
  137.             return(scount(ttyp->isem));
  138.         default:
  139.             return(SYSERR);
  140.     }
  141.     return(OK);
  142. }
  143. /* ttygetc.c - ttygetc  p. 164 */
  144.  
  145. # include    <conf.h>
  146. # include    <kernel.h>
  147. # include    <tty.h>
  148. # include    <io.h>
  149. # include    <slu.h>
  150.  
  151. /*-----------------------------------------------------------------------------
  152.  *  ttygetc  --  read one character from a tty device
  153.  *-----------------------------------------------------------------------------
  154.  */
  155. ttygetc(devptr)
  156.     struct devsw    *devptr;
  157. {
  158.     char    ps;
  159.     int    ch;
  160.     struct tty    *iptr;
  161.  
  162.     disable(ps);
  163.     iptr = &tty[devptr->dvminor];
  164.     wait(iptr->isem);        /* wait for a character in buff      */
  165.     ch = iptr->ibuff[iptr->itail++];
  166.     if (iptr->itail == IBUFLEN)
  167.         iptr->itail = 0;
  168.     restore(ps);
  169.     return(ch);
  170. }
  171. /* ttyiin.c - ttyiin, erase1, eputc, echoch  p. 174 */
  172.  
  173. # include    <conf.h>
  174. # include    <kernel.h>
  175. # include    <tty.h>
  176. # include    <io.h>
  177. # include    <slu.h>
  178.  
  179. /*-----------------------------------------------------------------------------
  180.  *  ttyiin  --  lower-half tty device driver for input interrupts
  181.  *-----------------------------------------------------------------------------
  182.  */
  183. INTPROC    ttyiin(iptr)
  184.     register struct tty    *iptr;    /* pointer to tty block              */
  185. {
  186.     register struct csr    *cptr;
  187.     register int    ch;
  188.     Bool    cerr;
  189.     int    ct;
  190.  
  191.     cptr = iptr->ioaddr;
  192.     if (iptr->imode == IMRAW)  {
  193.         if (scount(iptr->isem) >= IBUFLEN)  {
  194.             ch = cptr->crbuf;
  195.             return;
  196.         }
  197.         if ((ch=cptr->crbuf)&SLUERMASK)    /* character error   */
  198.             iptr->ibuff[iptr->ihead++] = (ch&SLUCHMASK) | IOCHERR;
  199.         else            /* normal read complete              */
  200.             iptr->ibuff[iptr->ihead++] = ch & SLUCHMASK;
  201.         if (iptr->ihead >= IBUFLEN)    /* wrap buffer pointer       */
  202.             iptr->ihead = 0;
  203.         signal(iptr->isem);
  204.     }  else  {            /* cbreak or cooked mode             */
  205.         cerr = ((ch=cptr->crbuf)&SLUERMASK) ? IOCHERR : 0;
  206.         ch &= SLUCHMASK;
  207.         if (ch == RETURN && iptr->icrlf)
  208.             ch = NEWLINE;
  209.         if (iptr->oflow)  {
  210.             if (ch == iptr->ostart)  {
  211.                 iptr->oheld = FALSE;
  212.                 cptr->ctstat = SLUENABLE;
  213.                 return;
  214.             }
  215.             if (ch == iptr->ostop)  {
  216.                 iptr->oheld = TRUE;
  217.                 return;
  218.             }
  219.         }
  220.         iptr->oheld = FALSE;
  221.         if (iptr->imode == IMCBREAK)  {    /* cbreak mode               */
  222.             if (scount(iptr->isem) >= IBUFLEN)  {
  223.                 eputc(iptr->ifullc, iptr, cptr);
  224.                 return;
  225.             }
  226.             iptr->ibuff[iptr->ihead++] = ch | cerr;
  227.             if (iptr->ihead >= IBUFLEN)
  228.                 iptr->ihead = 0;
  229.             if (iptr->iecho)
  230.                 echoch(ch, iptr, cptr);
  231.             if (scount(iptr->isem) < IBUFLEN)
  232.                 signal(iptr->isem);
  233.         }  else  {            /* cooked mode               */
  234.             if (ch == iptr->ikillc && iptr->ikill)  {
  235.                 iptr->ihead -= iptr->icursor;
  236.                 if (iptr->ihead < 0)
  237.                     iptr->ihead += IBUFLEN;
  238.                 iptr->icursor = 0;
  239.                 eputc(RETURN, iptr, cptr);
  240.                 eputc(NEWLINE, iptr, cptr);
  241.                 return;
  242.             }
  243.             if (ch == iptr->ierasec && iptr->ierase)  {
  244.                 if (iptr->icursor > 0)  {
  245.                     iptr->icursor--;
  246.                     erase1(iptr, cptr);
  247.                 }
  248.                 return;
  249.             }
  250.             if (ch == NEWLINE || ch == RETURN)  {
  251.                 if (iptr->iecho)
  252.                     echoch(ch, iptr, cptr);
  253.                 iptr->ibuff[iptr->ihead++] = ch | cerr;
  254.                 if (iptr->ihead >= IBUFLEN)
  255.                     iptr->ihead = 0;
  256.                 ct = iptr->icursor+1;    /* +1 for \n or \r   */
  257.                 iptr->icursor = 0;
  258.                 signaln(iptr->isem, ct);
  259.                 return;
  260.             }
  261.             ct = scount(iptr->isem);
  262.             ct = ct < 0 ? 0 : ct;
  263.             if ((ct + iptr->icursor) >= IBUFLEN-1)  {
  264.                 eputc(iptr->ifullc,iptr,cptr);
  265.                 return;
  266.             }
  267.             if (iptr->iecho)
  268.                 echoch(ch,iptr,cptr);
  269.             iptr->icursor++;
  270.             iptr->ibuff[iptr->ihead++] = ch | cerr;
  271.             if (iptr->ihead >= IBUFLEN)
  272.                 iptr->ihead = 0;
  273.         }
  274.     }
  275. }
  276.  
  277. /*-----------------------------------------------------------------------------
  278.  *  erase1  --  erase one character honoring erasing backspace
  279.  *-----------------------------------------------------------------------------
  280.  */
  281. LOCAL    erase1(iptr,cptr)
  282.     struct tty    *iptr;
  283.     struct csr    *cptr;
  284. {
  285.     char    ch;
  286.  
  287.     if (--(iptr->ihead) < 0)
  288.         iptr->ihead += IBUFLEN;
  289.     ch = iptr->ibuff[iptr->ihead];
  290.     if (iptr->iecho)  {
  291.         if (ch < BLANK || ch == 0177)  {
  292.             if (iptr->evis)  {
  293.                 eputc(BACKSP,iptr,cptr);
  294.                 if (iptr->ieback)  {
  295.                     eputc(BLANK,iptr,cptr);
  296.                     eputc(BACKSP,iptr,cptr);
  297.                 }
  298.             }
  299.             eputc(BACKSP,iptr,cptr);
  300.             if (iptr->ieback)  {
  301.                 eputc(BLANK,iptr,cptr);
  302.                 eputc(BACKSP,iptr,cptr);
  303.             }
  304.         }  else  {
  305.             eputc(BACKSP,iptr,cptr);
  306.             if (iptr->ieback)  {
  307.                 eputc(BLANK,iptr,cptr);
  308.                 eputc(BACKSP,iptr,cptr);
  309.             }
  310.         }
  311.     }  else
  312.         cptr->ctstat = SLUENABLE;
  313. }
  314.  
  315. /*-----------------------------------------------------------------------------
  316.  *  echoch  --  echo a character with visual and ocrlf options
  317.  *-----------------------------------------------------------------------------
  318.  */
  319. LOCAL    echoch(ch, iptr, cptr)
  320.     char    ch;            /* character to echo                 */
  321.     struct tty    *iptr;        /* pntr to I/O block for this devptr */
  322.     struct cst    *cptr;        /* csr address for this devptr       */
  323. {
  324.     if ((ch == NEWLINE || ch == RETURN) && iptr->ecrlf)  {
  325.         eputc(RETURN,iptr,cptr);
  326.         eputc(NEWLINE,iptr,cptr);
  327.     }  else if ((ch < BLANK || ch == 0177) && iptr->evis)  {
  328.         eputc(UPARROW,iptr,cptr);
  329.         eputc(ch+0100,iptr,cptr);    /* make it printable         */
  330.     }  else  {
  331.         eputc(ch,iptr,cptr);
  332.     }
  333.     cptr->ctstat = SLUENABLE;
  334. }
  335.  
  336. /*-----------------------------------------------------------------------------
  337.  *  eputc  --  put one character in the echo queue
  338.  *-----------------------------------------------------------------------------
  339.  */
  340. LOCAL    eputc(ch,iptr,cptr)
  341.     char    ch;
  342.     struct tty    *iptr;
  343.     struct csr    *cptr;
  344. {
  345.     iptr->ebuff[iptr->ehead++] = ch;
  346.     if (iptr->ehead >= EBUFLEN)
  347.         iptr->ehead = 0;
  348.     cptr->ctstat = SLUENABLE;
  349. }
  350. /* ttyinit.c - ttyinit   p. 174 */
  351.  
  352. # include    <conf.h>
  353. # include    <kernel.h>
  354. # include    <tty.h>
  355. # include    <io.h>
  356. # include    <slu.h>
  357.  
  358. /*-----------------------------------------------------------------------------
  359.  *  ttyinit  --  initialize buffers and modes for a tty line
  360.  *-----------------------------------------------------------------------------
  361.  */
  362. ttyinit(devptr)
  363.     struct devsw    *devptr;
  364. {
  365.     register struct tty    *iptr;
  366.     register struct csr    *cptr;
  367.     int    i, junk, isconsole;
  368.  
  369.     /* set up interrupt vector and interrupt dispatch table */
  370.  
  371.     iptr = &tty[devptr->dvminor];
  372.     iosetvec(devptr->dvnum, iptr, iptr);
  373.  
  374.     devptr->dvioblk = iptr;        /* fill tty control block            */
  375.     isconsole = (devptr->dvnum == CONSOLE);    /* make console cooked       */
  376.     iptr->ioaddr = devptr->dvcsr;    /* copy in csr address               */
  377.     iptr->ihead = iptr->itail = 0;    /* empty input queue                 */
  378.     iptr->isem = screate(0);    /* chars read so far = 0             */
  379.     iptr->osem = screate(OBUFLEN);    /* buffer available = all            */
  380.     iptr->odsend = 0;        /* sends delayed so far              */
  381.     iptr->ohead = iptr->otail = 0;    /* output queue empty                */
  382.     iptr->ehead = iptr->etail = 0;    /* echo queue empty                  */
  383.     iptr->imode = (isconsole ? IMCOOKED : IMRAW);
  384.     iptr->iecho = iptr->evis = isconsole;    /* echo console input        */
  385.     iptr->ierase = iptr->ieback = isconsole;/* console honors erase      */
  386.     iptr->ierasec = BACKSP;        /*   using ^h                        */
  387.     iptr->ecrlf = iptr->icrlf = isconsole;    /* map RETURN on input       */
  388.     iptr->ocrlf = iptr->oflow = isconsole;
  389.     iptr->ikill = isconsole;    /* set line kill == @                */
  390.     iptr->ikillc = ATSIGN;
  391.     iptr->oheld = FALSE;
  392.     iptr->ostart = STRTCH;
  393.     iptr->ostop = STOPCH;
  394.     iptr->icursor = 0;
  395.     iptr->ifullc = TFULLC;
  396.     
  397.     cptr = (struct csr *)devptr->dvcsr;
  398.     junk = cptr->crbuf;        /* clear receiver and..              */
  399.     cptr->crstat = SLUENABLE;       /*  enable input interrupts          */
  400.     cptr->ctstat = SLUDISABLE;    /* disable output interrupts         */
  401. }
  402.